package br.ufrj.dcc.trabxml;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.rpc.ServiceException;
import javax.xml.xpath.XPathExpressionException;

import org.apache.axis.client.AdminClient;
import org.xml.sax.SAXException;

import br.ufrj.dcc.trabxml.controller.CreateBook;
import br.ufrj.dcc.trabxml.controller.DeleteBook;
import br.ufrj.dcc.trabxml.controller.FindBooks;
import br.ufrj.dcc.trabxml.controller.LendBook;
import br.ufrj.dcc.trabxml.controller.ReturnBook;
import br.ufrj.dcc.trabxml.controller.ShowHistory;
import br.ufrj.dcc.trabxml.manager.HistoryManager;
import br.ufrj.dcc.trabxml.manager.XmlManager;
import br.ufrj.dcc.trabxml.util.EXistConnection;
import br.ufrj.dcc.trabxml.util.Properties;
import br.ufrj.dcc.trabxml.view.FormMain;

public class TrabXML {

    /**
     * @param args
     * @throws ServiceException 
     * @throws ParserConfigurationException 
     * @throws SAXException 
     * @throws IOException 
     * @throws URISyntaxException 
     * @throws XPathExpressionException 
     */
    public static void main(String[] args) throws Exception {
        initProperties();
        System.out.println(Properties.instance.getProperty("dbhost"));
        System.out.println(Properties.instance.getProperty("dbport"));
        System.out.println(Properties.instance.getProperty("dbuser"));
        System.out.println(Properties.instance.getProperty("dbpassword"));
/*        EXistConnection connection = new EXistConnection();
        connection.connect();
        String xml = connection.gainFile(EXistConnection.MY_CATALOG);
        connection.disconnect();
        System.out.println(XmlUtil.prettyPrint(xml));
        BookManager manager = new BookManager();
        manager.importBook("0262032937");
        manager.listBooks("");*/
        int myGroup = Integer.parseInt(Properties.instance.getProperty("mygroup"));        
        MyConnector.create(Integer.parseInt(Properties.instance.getProperty("port"+myGroup)));        
        /*if (args.length > 0) { //para testar muitas solicitacoes
        	while(true) {
        		try {
	        		String xml = MySender.send("127.0.0.1", 3000, XmlManager.createCatalogRequest(1));
	        		System.out.println(xml);
        		} catch (Exception e) {
        			e.printStackTrace();
        		}
        	}
        }*/
        int numGroups = Integer.parseInt(Properties.instance.getProperty("groups"));
        Collection<String> groups = new Vector<String>();
        for(int i = 1; i <= numGroups; i++) {
            groups.add(Integer.toString(i));
        }
        FormMain frmMain = new FormMain(myGroup,groups);
        frmMain.setFindBookListener(new FindBooks(frmMain));
        frmMain.setNewBookListener(new CreateBook());
        frmMain.setShowHistoryListener(new ShowHistory());
        frmMain.setRemoveBookListener(new DeleteBook(frmMain));
        frmMain.setLendBookListener(new LendBook(frmMain));
        frmMain.setReturnBookListener(new ReturnBook(frmMain));
        frmMain.setVisible(true);
        //MySender.send("192.168.0.100", 3000, "<root id='1'></root>");
    }
    
    private static void initProperties() {
        if (Properties.instance.getProperty("dbhost") == null) {
            Properties.instance.setProperty("dbhost","localhost");
        }
        if (Properties.instance.getProperty("dbport") == null) {
            Properties.instance.setProperty("dbport","8080");
        }
        if (Properties.instance.getProperty("dbuser") == null) {
            Properties.instance.setProperty("dbuser","guest");
        }
        if (Properties.instance.getProperty("dbpassword") == null) {
            Properties.instance.setProperty("dbpassword","guest");
        }
    }

}
